package com.dragon.modules.yuegou.client.wechat_mini.client.impl;

import com.dragon.modules.yuegou.client.wechat_mini.client.AuthClient;
import com.dragon.modules.yuegou.client.wechat_mini.conn.IMiniClient;
import com.dragon.modules.yuegou.client.wechat_mini.response.Code2SessionResponse;
import com.dragon.modules.yuegou.client.wechat_mini.response.TokenResponse;
import com.dragon.modules.yuegou.client.wechat_mini.response.UnionIdResponse;

public class AuthClientImpl implements AuthClient {

	private final IMiniClient client;

	public AuthClientImpl(IMiniClient client) {
		this.client = client;
	}

	@Override
	public Code2SessionResponse code2Session(String appid, String secret, String jsCode, String grantType) {
		return client.get("/sns/jscode2session")
				.parameter("appid", appid)
				.parameter("secret", secret)
				.parameter("js_code", jsCode)
				.parameter("grant_type", grantType)
				.execute(Code2SessionResponse.class);
	}

	@Override
	public UnionIdResponse getPaidUnionId(String accessToken, String openid, String transactionId) {
		return client.get("/wxa/getpaidunionid")
				.accessToken(accessToken)
				.parameter("openid", openid)
				.parameter("transaction_id", transactionId)
				.execute(UnionIdResponse.class);
	}

	@Override
	public UnionIdResponse getPaidUnionId(String accessToken, String openid, String outTradeNo, String mchId) {
		return client.get("/wxa/getpaidunionid")
				.accessToken(accessToken)
				.parameter("openid", openid)
				.parameter("out_trade_no", outTradeNo)
				.parameter("mch_id", mchId)
				.execute(UnionIdResponse.class);
	}

	@Override
	public TokenResponse getAccessToken(String appid, String secret, String grantType) {
		return client.get("/cgi-bin/token")
				.parameter("appid", appid)
				.parameter("secret", secret)
				.parameter("grant_type", grantType)
				.execute(TokenResponse.class);
	}

}
